package com.example.peanutsubway.dao.impl;

import com.example.peanutsubway.dao.ChannelSortDao;
import com.example.peanutsubway.entity.ChannelSort;
import com.example.peanutsubway.entity.PageInfo;
import com.example.peanutsubway.util.JDBCUtils;
import com.example.peanutsubway.vo.ChannelSortParentName;


import java.util.List;

/**
 * @Author：liyangsong
 * @Version：1.0
 * @Date：2021/10/13-16:47
 * @Since:jdk1.8
 * @Description:
 */


public class ChannelSortDaoImpl implements ChannelSortDao {

    @Override
    public List<ChannelSortParentName> showChannelSort(ChannelSort channelSort, PageInfo pageInfo) {
        StringBuilder sql = new StringBuilder("SELECT pn.*,p.cname fname from pn_channel_sort pn, pn_channel_sort p where p.cid=pn.fid and pn.status = 0");
        if (channelSort != null) {
            if (channelSort.getCname() != null && !"".equals(channelSort.getCname())) {
                sql.append(" and pn.cname like '%" + channelSort.getCname() + "%'  ");
            }
        }
        sql.append(" limit ?,?");
        return JDBCUtils.find(sql.toString(), ChannelSortParentName.class, pageInfo.getStartIndex(), pageInfo.getPageNum());
    }

    /**
     * 渠道总数
     *
     * @param channelSort
     * @return
     */
    @Override
    public Integer count(ChannelSort channelSort) {
        StringBuilder sql = new StringBuilder("select count(1) from pn_channel_sort pn, pn_channel_sort p where p.cid = pn.fid and pn.status = 0");
        if (channelSort != null) {
            if (channelSort.getCname() != null && !"".equals(channelSort.getCname())) {
                sql.append(" and pn.cname like '%" + channelSort.getCname() + "%'  ");
            }
        }
        return JDBCUtils.count(sql.toString()).intValue();
    }

    @Override
    public List<ChannelSort> ChannelSortfid(ChannelSort channelSort) {
        String sql = "select * from pn_channel_sort where fid =0";
        return JDBCUtils.find(sql, ChannelSort.class);
    }

    @Override
    public List<ChannelSort> channelSortSid(Integer fid) {
        String sql = "select * from pn_channel_sort where fid = ? ";
        return JDBCUtils.find(sql,ChannelSort.class,fid);
    }

    @Override
    public Boolean addChannelSort(ChannelSort channelSort) {
        StringBuilder sql = new StringBuilder("INSERT into pn_channel_sort VALUES(null,?,?,NOW(),?,NULL)");
        return JDBCUtils.insert(sql.toString(), channelSort.getFid(), channelSort.getCname(), channelSort.getStatus()) > 0;
    }

    @Override
    public Boolean deleChannelSort(Integer cid) {
        String sql = "update pn_channel_sort set status = 1 where cid = ?";
        return JDBCUtils.update(sql, cid) > 0;
    }

    @Override
    public Boolean updateChannelSort(ChannelSort channelSort) {
        String sql = "update pn_channel_sort set cname = ? , fid = ? ,status=?,des=? where cid = ?";
        return JDBCUtils.update(sql, channelSort.getCname(), channelSort.getFid(), channelSort.getStatus(), channelSort.getDes(), channelSort.getCid()) > 0;
    }

    @Override
    public List<ChannelSort> allChannelSort(ChannelSort channelSort) {
        String sql ="select * from  pn_channel_sort ";
        return JDBCUtils.find(sql,ChannelSort.class);
    }

}
